package com.csdj.server.controller;


import com.csdj.server.pojo.Admin;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.pojo.Role;
import com.csdj.server.service.IAdminRoleService;
import com.csdj.server.service.IAdminService;
import com.csdj.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZouLi
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    /**
     * 注入 AdminService
     **/
    @Autowired
    private IAdminService adminServiceImpl;

    /**
     * 注入 AdminRoleService
     **/
    @Autowired
    private IAdminRoleService adminRoleServiceImpl;

    /**
     * 注入RoleService
     **/
    @Autowired
    private IRoleService roleServiceImpl;

    @ApiOperation(value = "模糊查询操作员")
    @GetMapping("/getAllAdmin")
    public List<Admin> getAllAdmin(String keyword) {
        return adminServiceImpl.getAllAdmin(keyword);
    }

    @ApiOperation(value = "修改操作员信息")
    @PutMapping("/updateAdmin")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if (adminServiceImpl.updateById(admin)){
            return RespBean.success("修改成功！");
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "删除操作员信息")
    @DeleteMapping("/deleteAdmin/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id){
        if (adminServiceImpl.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "添加用户角色信息")
    @PostMapping("/addAdminRole")
    public RespBean addAdminRole(Integer aId, Integer[] rIds){
        return adminRoleServiceImpl.updateAdminRole(aId, rIds);
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/getAdminRole")
    public List<Role> getAllRole(){
        return roleServiceImpl.list();
    }
}
